package com.heima.article.controller.v1;

import com.heima.article.service.ApArticleService;
import com.heima.common.constants.ArticleConstants;
import com.heima.common.exception.CustomException;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.dtos.ArticleInfoDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.vos.LoadBehaviorInfoVO;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author brianxia
 * @version 1.0
 * @date 2022-10-19 10:15
 */
@RestController
@RequestMapping("/api/v1/article")
public class ArticleHomeController {

    @Autowired
    private ApArticleService apArticleService;

    @PostMapping("/load")
    public ResponseResult load(@RequestBody ArticleHomeDto dto){
        return ResponseResult.okResult(apArticleService.load2(dto,null,true));
    }

    @PostMapping("/loadmore")
    public ResponseResult loadmore(@RequestBody ArticleHomeDto dto){
        //System.out.println("123");
        if(dto.getMinBehotTime() == null){
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }
        return ResponseResult.okResult(apArticleService.load2(dto, ArticleConstants.LOADTYPE_LOAD_MORE,false));
    }

    @PostMapping("/loadnew")
    public ResponseResult loadnew(@RequestBody ArticleHomeDto dto){
        if(dto.getMaxBehotTime() == null){
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID,"加载更新参数异常！");
        }
        return ResponseResult.okResult(apArticleService.load2(dto,ArticleConstants.LOADTYPE_LOAD_NEW,false));
    }

    @GetMapping("/test")
    public String test(){
        return "123";
    }

    @PostMapping("/save")
    public ResponseResult saveArticle(@RequestBody ArticleDto dto){
        //参数校验
        Long id = apArticleService.saveArticle(dto);
        return ResponseResult.okResult(id);
    }


    @PostMapping("/load_article_behavior")
    public ResponseResult loadArticleBehavior(@RequestBody ArticleInfoDto dto){
        //参数校验

        if(dto.getAuthorId() == null || dto.getArticleId() == null){
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }

        LoadBehaviorInfoVO vo = apArticleService.loadArticleBehavior(dto);
        return ResponseResult.okResult(vo);
    }
}
